package cn.rxmind.dlicense.core.validator;

import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.event.DLicenseEventPublisher;
import cn.rxmind.dlicense.core.event.DValidateEvent;

/**
 * 抽象校验器，
 * TODO 这里依赖Spring的事件发布器是不明智的，应当自定义事件发布器
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/26 2:53 下午
 */
public abstract class AbstractLicenseValidator implements DLicenseValidator {

    protected DLicenseEventPublisher eventPublisher;

    @Override
    public final ValidateResult validateLicense(DLicenseContent content) {

        ValidateResult result = validateResult(content);
        if(this.eventPublisher != null){
            this.eventPublisher.publish(new DValidateEvent(result.getDescription(),this.description()));
        }
        if(!result.valid()){
            ifInvalid(result);
        }else {
            ifValid(result);
        }

        return result;

    }

    @Override
    public void setPublisher(DLicenseEventPublisher publisher) {
        this.eventPublisher = publisher;
    }

    /**
     * 只用于获得校验结果
     *
     * @param content
     * @return
     */
    protected abstract ValidateResult validateResult(DLicenseContent content);
}
